<!-- 无人车管理 -->
<template>
  <div class="container">
    <div class="btns">
      <el-form :inline="true" :model="formData" class="demo-form-inline">
        <el-form-item>
          <el-button type="primary" @click="showDialog">新增</el-button>
        </el-form-item>
        <el-select v-model="formData.carstatus" clearable placeholder="请选择无人车状态">
          <el-option v-for="item in carStatusList" :key="item.id" :label="item.status" :value="item.value">
          </el-option>
        </el-select>
        <el-form-item>
          <el-button type="primary" @click="getCarData">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 新增/编辑的弹出框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="dialogForm">
        <el-form-item label="无人车编号" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.vehicle_id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="无人车名称" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="无人车状态" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.status" placeholder="请选择状态">
            <el-option v-for="item in carStatusList" :key="item.id" :label="item.status" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="当前位置" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.location" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item lable="最后维护时间" :label-width="formLabelWidth">
          <el-date-picker v-model="dialogForm.last_maintenance_date" type="date" value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCar">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 表格区域 -->
    <el-table :data="carDataList" style="width: 100%">
      <el-table-column type="index" :index="indexMethod()" width="50">
      </el-table-column>
      <el-table-column prop="vehicle_id" label="无人车编号">
      </el-table-column>
      <el-table-column prop="name" label="无人车名称">
      </el-table-column>
      <el-table-column prop="location" label="位置">
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'available'">可用</el-tag>
          <el-tag type="success" v-else-if="scope.row.status === 'in_use'">使用中</el-tag>
          <el-tag type="danger" v-else>维修中</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间">
      </el-table-column>

      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row)">
            <el-button type="danger" size="mini" slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="paginationData.page" :page-sizes="[10, 20, 30, 40]" :page-size="paginationData.page_size"
      layout="total, sizes, prev, pager, next, jumper" :total="paginationData.total">
    </el-pagination>
  </div>
</template>
<script>
import { get, post, Delete, put } from '@/utils/request.js'
import { Message } from 'element-ui'
export default {
  data() {
    return {
      dialogFormVisible: false, // 新增/编辑的弹出框
      // 查询条件
      formData: {
        carstatus: '',
      },
      formLabelWidth: '120px',
      // 弹出层表单数据
      dialogForm: {
        vehicle_id: '', // 车辆编号
        name: '', //车辆名称
        status: 'available', // 状态
        location: '', //当前位置
        last_maintenance_date: '' // 最后维护时间
      },
      dialogTitle: '新增无人车', // 弹出层标题
      carStatusList: [
        { id: 1, status: '空闲', value: 'available' },
        { id: 2, status: '使用中', value: 'in_use' },
        { id: 3, status: '维修中', value: 'maintenance' },
      ],
      carDataList: [],
      // 分页数据
      paginationData: {
        page: 1,
        page_size: 10,
        total: 0
      }
    }
  },
  methods: {
    //计算序号
    indexMethod() {
      let temp = this.paginationData.page_size * (this.paginationData.page - 1)
      return temp + 1
    },
    // 获取无人车信息
    getCarData() {
      get('/accounts/vehicles/', {
        page: this.paginationData.page,
        page_size: this.paginationData.page_size,
        status: this.formData.carstatus
      }).then(res => {
        this.carDataList = res.data
        this.paginationData = res.pagination
      }).catch(err => {
        console.log(err)
      })
    },
    // 添加无人车信息
    addCarData() {
      post('/accounts/vehicles/', this.dialogForm).then(res => {
        // console.log('创建成功：',res)
        this.getCarData()
        // Message.success(res.message)
      }).catch(err => {
        console.log(err)
      })
    },
    // 更新无人车信息 id:无人车id
    updateCarData(id) {
      put(`/accounts/vehicles/${id}/`, this.dialogForm).then(res => {
        console.log(res)
        this.getCarData()
        Message.success(res.message)
      }).catch(err => {
        console.log(err)
        Message.error(err.message)
      })
    },
    // 删除无人车信息
    deleteCarData(id) {
      Delete('/accounts/vehicles/' + id + '/').then(res => {
        // console.log(res)
        this.getCarData()
        Message.success(res.message)
      })
    },


    //点击新增按钮
    showDialog() {
      this.dialogTitle = '新增无人车'
      this.dialogForm = {}
      this.dialogFormVisible = true
    },
    // 点击确定按钮
    addCar() {
      if (this.dialogTitle == '新增无人车') {
        this.addCarData()
        this.getCarData()
        this.dialogFormVisible = false
      } else {
        this.updateCarData(this.dialogForm.id)
        this.getCarData()
        this.dialogFormVisible = false
      }
    },
    // 点击编辑按钮
    handleEdit(row) {
      this.dialogTitle = '编辑无人车'
      this.dialogFormVisible = true
      this.dialogForm = row
    },

    // 点击删除按钮
    handleDelete(row) {
      this.deleteCarData(row.id)
      // this.deleteCarData(row.id)
    },

    // 分页事件
    handleSizeChange(val) {
      this.paginationData.page_size = val
      this.getCarData()
    },
    handleCurrentChange(val) {
      this.paginationData.page = val
      this.getCarData()
    }

  },
  created() {
    this.getCarData()
  },
}
</script>
<style scoped></style>